package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.LocationService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.entity.driver.DriverSet;
import com.atguigu.daijia.model.form.map.OrderServiceLocationForm;
import com.atguigu.daijia.model.form.map.UpdateDriverLocationForm;
import com.atguigu.daijia.model.form.map.UpdateOrderLocationForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class LocationServiceImpl implements LocationService {

	@Autowired
	private LocationFeignClient locationFeignClient;

	@Autowired
	private DriverInfoFeignClient driverInfoFeignClient;

	//更新司机经纬度位置
	@Override
	public Boolean updateDriverLocation(UpdateDriverLocationForm updateDriverLocationForm) {
		//开启接单了才能更新司机接单位置
		DriverSet driverSet = driverInfoFeignClient.getDriverSet(updateDriverLocationForm.getDriverId()).getData();
		//判断:如果司机开始接单,更新位置信息
		if(driverSet.getServiceStatus().intValue() == 1) {
			return locationFeignClient.updateDriverLocation(updateDriverLocationForm).getData();
		} else {
			//司机没有接单
			throw new GuiguException(ResultCodeEnum.NO_START_SERVICE);
		}
	}

	//机赶往代驾起始点：更新订单位置到Redis缓存
	@Override
	public Boolean updateOrderLocationToCache(UpdateOrderLocationForm updateOrderLocationForm) {
		return locationFeignClient.updateOrderLocationToCache(updateOrderLocationForm).getData();
	}

	//开始代驾服务：保存代驾服务订单位置
	@Override
	public Boolean saveOrderServiceLocation(List<OrderServiceLocationForm> orderLocationServiceFormList) {
		return locationFeignClient.saveOrderServiceLocation(orderLocationServiceFormList).getData();
	}
}
